<?php
require("includes/constantesMvial.php");

$tplCatcol=new TplLoad;
$where_clause="";
global $cat_tbl,$catid,$set_cats_per_column,$fp,$set_yahoostyle_cat,$set_multilang_cat,$set_yahoostyle_numcat,$set_cat_sort;
global $set_yahoostyle_length,$cat_tpl,$set_yahoo_sort,$set_language,$set_rewrite,$set_multilang_cat,$set_region,$set_region_dropdown;
global $parentCatid;

//JMR: este es el path de la categoria
$categoryPath="";
$showAll=getParamInt("showAll",0);
$mainCat=getParamInt("catid",ID_CAT_COMPRA);
//logMsg("JMR:","CATID ES: ".$catid);
//JMR: Depending of the catid, is the tab selected
$tabSelected=1;
if($catid == ID_CAT_ALQUILER)
{ $tabSelected = 2; }
else if($catid == ID_CAT_SERVICIOS)
{ $tabSelected = 3; }
$tplCatcol->assign("tabSelected",$tabSelected);
//end JMR

if ($set_region && $set_region_dropdown=="1")
{
	if ($_SESSION["region"])
		$lSelected=round($_SESSION["region"]);
	if (getParamInt("region",""))
		$lSelected=getParamInt("region","");
	$tplCatcol->assign("options",getRegionTree(0,0,$lSelected));
	//$lRegOptions="<option value=''> -- ".LA_ADMIN_REGIONS_SELECT." -- </option>";
}

/*if ($catid==0) { */
if ($catid>=ID_CAT_COMPRA) {
	//$where_clause = "where cat_fatherid = 0 "; 
	$where_clause = "where cat_fatherid = $catid "; 
	if ($showAll==0){
		$where_clause = $where_clause . " and show_in_frontpage = 1"; 
	}
	$parentCatid = 0;
	}
else {
	$where_clause = "where cat_fatherid=$catid";  
	$parentCatid = $catid;
}

$sql = "select cat_id,cat_name,cat_description,cat_fatherid,cat_allow_ads,cat_total,cat_image";
if ($set_multilang_cat)
{
	$lLangSupportedArray=split(",",$set_multilang_cat);
	foreach ($lLangSupportedArray as $lItem)
		$sql.=",lang_" . $lItem;
}
$lOrder="cat_name";

//if ($set_cat_sort)
	//$lOrder="order_number asc, $set_cat_sort";
	
$sql.= " from $cat_tbl $where_clause order by $lOrder";

$result = q ($sql);
$i=1;
$catid_list="";
if (empty($set_cats_per_column) || $set_cats_per_column==0)
{
	echo "<h4>Your configuration does not contain number of columns per page setting or it is zero, which isn�t possible. Did you read the step
	in the manual and the last page of install that asked you to save settings in both General <u>AND</u> Option settings?</h4>";
}
$td_size=round(100/$set_cats_per_column,1);
$tplCatcol->assign("CAT_PER_ROW",$set_cats_per_column);
$tplCatcol->assign("TD_SIZE",$td_size);
while($row=mysql_fetch_array($result))
{
	$catid=$row["cat_id"];
 	$catid_list.=$catid . ",";
 	// Load template variables
  	$fArray[$catid]["IMAGE"] = $row["cat_image"];
  	$fArray[$catid]["TOTAL_ADS"] = $row["cat_total"];

  	$lCatName="";
	if ($set_multilang_cat)
		$lCatName = $row["lang_" . $set_language];
	if (!$lCatName)
		$lCatName = $row["cat_name"]; // Last resort, use original name..
  	
  	$fArray[$catid]["CATEGORYNAME"] = $lCatName;	
  	$fArray[$catid]["ALLOWADS"]		= $row["cat_allow_ads"];
  	$fArray[$catid]["CATFATHERID"] 	= $row["cat_fatherid"];
  	$fArray[$catid]["CATID"] 	= $row["cat_id"];
  
  	$fArray[$catid]["CATDESCRIPTION"] = $row["cat_description"];
  	$fArray[$catid]["NB"] 	= $i;
  
  
  	if ($frontpage==2) {
  		$fName="add_ad_cat";
  		$fArray[$catid]["URL"] = "<a href='" .$fName . ".php?catid=".$row["cat_id"] ."'>";
  		$fArray[$catid]["/URL"] = "</a>";
  	}
  	else {$fName="index";}
  	{
  		//$fArray[$catid]["URL"] = "<a href='" .mvialLinkCat($row["cat_id"]) ."' style='color:#DB3928;font-size:11px;line-height:normal'>";
  		$fArray[$catid]["URL"] = "<a href='/" . guiones(busca_existencia_dato("mvial_category","cat_id",$row["cat_fatherid"],"1","1","cat_name")). '/' .$row["cat_id"] .'-'. guiones($row["cat_name"]) ."' style='color:#DB3928;font-size:11px;line-height:normal'>";
  		$fArray[$catid]["/URL"] = "</a>";
  	}
  
 	// Increase categorycounter by one
  	$catno = $catno + 1;
  
  	// If max categories for this column is reached
  

  $fArray[$catid]["REMAIN_TD"]=$set_cats_per_column-$catno;
  if ($catno == $set_cats_per_column)
  {
  	$fArray[$catid]["BREAK"] = 1;
  	$catno=0;
  }
  $i++;
}
$catid_list=substr($catid_list,0,-1);
$total_count = count($fArray);

if ($set_yahoostyle_cat && !empty($catid_list))
{
	$lSubOrder="cat_name";	
	
	$sql="select cat_name,cat_fatherid,cat_id,cat_total";
	if ($set_multilang_cat)
	{
		$lLangSupportedArray=split(",",$set_multilang_cat);
		foreach ($lLangSupportedArray as $lItem)
			$sql.=",lang_" . $lItem;
	}
	$sql.=" from $cat_tbl where cat_fatherid in ($catid_list) order by $lSubOrder";	

	$res=q($sql);
	$t=0;
	$prev_cat=0;
	
	while ($row=mysql_fetch_array($res))
	{	
		$catfatherid=$row["cat_fatherid"];
		$catid=$row["cat_id"];
		$total=$row["cat_total"];
		if (empty($lCountArray[$catfatherid]))
			$lCountArray[$catfatherid]=1;
		else
			$lCountArray[$catfatherid]=$lCountArray[$catfatherid] + 1;
		
		if ($lCountArray[$catfatherid]<=$set_yahoostyle_numcat)
		{
			$lCatName="";
			// Check if user selected category exists
			if ($set_multilang_cat)
				$lCatName = $row["lang_" . $set_language];
			if (!$lCatName)
				$lCatName = $row["cat_name"]; // Last resort, use original name..
				//JMR: Change this
				if($catfatherid==ID_CAT_REOUESTOS_ALQUILER || $catfatherid==ID_CAT_REOUESTOS_COMPRA){
					$lCatName = str_ireplace("repuestos", "", $lCatName);
				}
			$fArray[$catfatherid]["SUBCATLIST"][$t]["CATNAME"]=dotString($lCatName,13/*$set_yahoostyle_length-3*/);
			$fArray[$catfatherid]["SUBCATLIST"][$t]["CATNAME_FULL"]=strEnc($lCatName);
			$fArray[$catfatherid]["SUBCATLIST"][$t]["NUM"]=$total;
			$fArray[$catfatherid]["SUBCATLIST"][$t]["CATID"]=$catid;
			$fArray[$catfatherid]["SUBCATLIST"][$t]["CATLINK"] = mvialLinkCat($catid);
			$t++;
            $total_count++;
		}
	}
	
}

if ($parentCatid < ID_CAT_COMPRA) //esta variable NO ESTA DEFINIDA en la front page. Uso esto para usar un style distinto
	$tplCatcol->assign("parentCatid", $parentCatid);
	
$tplCatcol->assign("total_count", $total_count);
$tplCatcol->assign("categories", $fArray);
$tplCatcol->assign("showAll", $showAll);
//JMR: Change 18-05-2009, antes se llamaba una sola vez, y a la variable mainCat se le asignaba $mainCat.
//Esto es para que se comporte como los TABS, siempre estan todas pero se muestrauna u otra al clikear
$tplCatcol->assign("mainCat", $mainCat);

if ($mainCat==0 || $mainCat==ID_CAT_COMPRA || $mainCat==ID_CAT_ALQUILER || $mainCat==ID_CAT_SERVICIOS ){	
	$tplCatcol->assign("front", 1);
}else{
	$categoryPath = getCategoryPath($mainCat);
	$tplCatcol->assign("front", 0);
}
if($mainCat==0) {
	$tplCatcol->assign("estoyEnFromt", 1);
}
$tplCatcol->assign("categoryPath", $categoryPath);
	
$tplCatcol->display("catcol.tpl");

?>
